﻿using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using Furion.DependencyInjection;
using Furion.FriendlyException;
using HOPU.OnlineSchool.Application.Teachers.Dtos;
using HOPU.OnlineSchool.Core;
using Mapster;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace HOPU.OnlineSchool.Application.Teachers.Services
{
    public class TeacherService : ITeacherService, ITransient
    {
        private readonly IRepository<EduTeacher> _repository;
        public TeacherService(IRepository<EduTeacher> repository)
        {
            _repository = repository;
        }

        public async Task AddAsync(TeacherAddInput input)
        {
            var model = input.Adapt<EduTeacher>();
            //通过雪花算法生成主键Id
            model.Id = YitIdHelper.NextId().ToString();
            await _repository.InsertAsync(model);
        }

        public async Task DelectAsync(string id)
        {
            var model = await _repository.FirstOrDefaultAsync(e => e.IsDeleted == 0 && e.Id == id);
            if (model==null)
            {
                throw Oops.Oh("哦，不好意思，该讲师已被删除了！");
            }
            else
            {
                model.IsDeleted = 1;
                model.GmtModified = DateTime.Now;
               await _repository.UpdateIncludeAsync(model,new string[] { 
                 nameof(model.IsDeleted),nameof(model.GmtModified)});
            }
        }

        public async Task<PagedList<TeacherListOutput>> GetPagedListAsync(int page, int limit, TeacherListInput input)
        {
            var query = _repository.Where(e => e.IsDeleted == 0);
            if (!string.IsNullOrWhiteSpace(input.Name))
            {
                query = query.Where(e => e.Name.Contains(input.Name));
            }
            if (input.Level>0)
            {
                query = query.Where(e => e.Level == input.Level);
            }
            if (input.Begin.HasValue)
            {
                query = query.Where(e => e.GmtCreate >= input.Begin.Value);
            }
            if (input.End.HasValue)
            {
                query = query.Where(e => e.GmtModified >= input.End.Value);
            }
            var list = await query.OrderByDescending(e=>e.GmtCreate).ToPagedListAsync(page, limit);
            return list.Adapt<PagedList<TeacherListOutput>>();
            
        }

        public async Task<TeacherDto> GetTeacherAsync(string id)
        {
            var model = await _repository.FirstOrDefaultAsync(t => t.Id == id && t.IsDeleted == 0);
            if (model == null)
                throw Oops.Oh("该讲师未找到");
            return model.Adapt<TeacherDto>();
        }

        public async Task UpdateAsync(string id, TeacherDto dto)
        {
            var exists = await _repository.AnyAsync(t => t.Id == id && t.IsDeleted == 0);
            if (!exists)
                throw Oops.Oh("该讲师未找到");
            var teacher = dto.Adapt<EduTeacher>();
            await teacher.UpdateExcludeAsync(new[] { nameof(teacher.GmtCreate), nameof(teacher.IsDeleted) });
        
         }
    }
}
